package com.wiegand.loader {
	import com.wiegand.event.ImageEvent;
	import com.wiegand.event.LoaderEvent;
	import com.wiegand.loader.ImageLoader;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;

	/**
	 * @author kwiegand
	 */
	 
	public class ImageLoader extends Sprite{

		private static var _instance : ImageLoader;

		private var _files : Array;						
		private var _data : Array;		
		private var _datanames : Array;		
		private var _names : Array;		
		private var _loader : Loader;		
		private var _i : int = 0;

		public static function getInstance() : ImageLoader{
			
			if( _instance == null )
				_instance = new ImageLoader( new SingletonEnforecer() );
			
			return _instance;
			
		}
		
		public function ImageLoader( blocker:SingletonEnforecer ):void{
			
			if ( blocker == null ) {
				
            	throw new Error("Error: Instantiation failed: Use ImageLoader.getInstance() instead of new.");
          
          	}
          	else{
          	
          		init();
          	
          	}
		
		}
		
		private function init() : void{
					
			_data = new Array();

			_datanames = new Array();

			_names = new Array();
			
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleComplete ); 
			
		}
		
		public function load( files : Array, names : Array = null, clear : Boolean = false ):void{
			
			if( clear ) clearData();
			
			if( names != null ) _names = names;
			
			_files = files;
			
			dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_START_LOADING ) );	
			
			_i = 0;
			
			loadFile();
			
		}
		
		private function loadFile() : void{
		
			if( _i < _files.length ){
								
				_loader.load( new URLRequest( _files[ _i ] ) );
				
			}
			else{
				
				dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_LOADING_COMPLETE ));	
				
			}
		
		}
		
		private function handleComplete( event : Event ) : void {
										
			try{	
						
				var file : String = String( _files[ _i ] );
				var format : String = file.slice( file.length - 4 );
				
				var img : Bitmap;
				
				if( format == ".swf" ){ 
					var bmpData : BitmapData = new BitmapData( _loader.content.width, _loader.content.height, true, 0x00ffffff );
					bmpData.draw( _loader.content, null, null, null, null, true );
					img = new Bitmap( bmpData );
				}
				else img = Bitmap( _loader.content );
				
				var index : uint = _data.push( img );						
				index--;			
										
				if( _names[ _i ] != undefined ){
				
					_datanames[ index ] = _names[ _i ];
										
					dispatchEvent( new ImageEvent( ImageEvent.EVENT_ONE_COMPLETE, index, _datanames[ index ] ) );
				
				}
				else{
				
					//no name
					
					dispatchEvent( new ImageEvent( ImageEvent.EVENT_ONE_COMPLETE, index ) );
				
				}
				
				_i++;
				loadFile();
				
				if( _i == 1 ) dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_FIRST_COMPLETE ));
				
			}
			catch ( e : TypeError){
								
			}
			
		}
		
		public function getImages() : Array{
			
			return _data;
			
		}
		
		public function getBitmap( n : int ) : Bitmap{			
			
			if( _data[ n ] != undefined ){
				
				return _data[ n ] as Bitmap;
				
			}
			else{
				
				throw new Error("Error: Image doesn't exist.");
          
				return null;
				
			}
			
		}
		
		public function getBitmapByName( s : String ) : Bitmap{			
			
			var b : Bitmap = null;
			
			for( var i : int = 0; i < _data.length; i++ ){
			
				if( _datanames[ i ] == s ) b = getBitmap( i );
			
			}
			
			
			return b;
			
		}

		public function getNames() : Array{			
						
			return _names;
		}
		
		public function getBitmapData( n : int ) : BitmapData{			
						
			if( _data[ n ] != undefined ){
				
				return ( _data[ n ] as Bitmap ).bitmapData ;
				
			}
			else{
				
				throw new Error("Error: Image doesn't exist.");
          
				return null;
				
			}
			
		}
				
		public function getBitmapDataByName( s : String ) : BitmapData{			
			
			var b : BitmapData = null;
			
			for( var i : int = 0; i < _data.length; i++ ){
				
				if( _datanames[ i ] == s ) b = getBitmapData( i );
			
			}
			
			return b;
			
		}
		
		public function clearData():void{
				
			if( _names != null ){
			
				for( var k : int = 0; k < _names.length; k++){
					
					_names[k] = null;
					
				}
				
				_names = new Array();
				
			}
				
			if( _datanames != null ){
			
				for( var l : int = 0; l < _datanames.length; l++){
					
					_datanames[l] = null;
					
				}
				
				_datanames = new Array();
				
			}
				
			if( _files != null ){
			
				for( var i : int = 0; i < _files.length; i++){
					
					_files[i] = null;
					
				}
				
				_files = new Array();
				
			}

			if( _data != null ){
	
				for( var j : int = 0; j < _data.length; j++){
					
					_data[j] = null;
					
				}	
						
				_data = new Array();
				
			}
		
		}
	}
}

internal class SingletonEnforecer {}
